package com.jy.adscm.zltx.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.service.JyUserService;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.QsIssueRecyclingQsfile;
import com.jy.adscm.zltx.service.QsIssueRecyclingQsfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文件发放回收记录 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-10-23
 */
@RestController
@RequestMapping("/adscm/zltx/qsIssueRecyclingQsfile")
public class QsIssueRecyclingQsfileController {
    @Autowired
    JyUserService jyUserService;
    @Autowired
    QsIssueRecyclingQsfileService qsIssueRecyclingQsfileService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @AdscmLogger("新增文件发放回收记录")
     @RequestMapping("/insert")
    public SimpleCoreResponse insert(@RequestBody QsIssueRecyclingQsfile  qsIssueRecyclingQsfile , HttpServletRequest request) throws AdscmSystemException {
         String tokenId = request.getParameter("tokenId");
         Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (token==null){
            throw  new AdscmSystemException("没找到登录信息");
        }
         qsIssueRecyclingQsfile.setIssuer(token.getUser().getId().toString());
        qsIssueRecyclingQsfile.setAcceptTime(new Date());
        qsIssueRecyclingQsfileService.insertQsIssueRecyclingQsfile(qsIssueRecyclingQsfile);
        return  new SimpleCoreResponse("200","新增成功",null);
    }
    @AdscmLogger("修改文件发放回收记录")
    @RequestMapping("/update")
    public  SimpleCoreResponse update(@RequestBody QsIssueRecyclingQsfile qsIssueRecyclingQsfile ,HttpServletRequest request)throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (token==null){
            throw  new AdscmSystemException("没找到登录信息");
        }
        if ( qsIssueRecyclingQsfile.getRecyclingVersionCode()!=null&&!"".equals(qsIssueRecyclingQsfile.getRecyclingVersionCode())){
            qsIssueRecyclingQsfile.setRecyclingPerson(token.getUser().getId().toString());
            qsIssueRecyclingQsfile.setRecyclingTime(new Date());
        }
         qsIssueRecyclingQsfileService.updateById(qsIssueRecyclingQsfile);
         return  new SimpleCoreResponse("200","修改成功",null);
    }
    @AdscmLogger("删除文件发放回收记录")
    @RequestMapping("/delete/{id}")
    public   SimpleCoreResponse delete(@PathVariable("id") Integer id)throws AdscmSystemException{
     qsIssueRecyclingQsfileService.deleteById(id);
     return  new SimpleCoreResponse("200","删除成功",null);
    }
    @AdscmLogger("查询文件发放回收记录")
    @RequestMapping("/selectByFileId/{fileId}")
    public  SimpleCoreResponse seletByFileId(@PathVariable Integer fileId)throws AdscmSystemException{

        List<QsIssueRecyclingQsfile> qsIssueRecyclingQsfiles = qsIssueRecyclingQsfileService.selectByFileId(fileId);
        return  new SimpleCoreResponse("200","查询成功",qsIssueRecyclingQsfiles);
    }
    @AdscmLogger("查询发放人")
    @RequestMapping("/queryUser")
    public  SimpleCoreResponse queryUser()throws AdscmSystemException{
        Wrapper<JyUser> wrapper = new EntityWrapper<>();
        wrapper.gt("id","1");
        List<JyUser> jyUsers = jyUserService.selectList(wrapper);
        return  new SimpleCoreResponse("200","查询成功",jyUsers);
    }

}

